<?php
class drag
{
	public $setting_table;
    public $template_table;
	public $pages;
	public $db;

	function drag()
	{
		global $db;
        $this->db = &$db;
        $this->setting_table = DB_PRE.'drag_setting';
        $this->template_table = DB_PRE.'drag_template';
	}

    /**
	 * 添加模板信息
	 *
	 * @param ARRAY() $data
	 * @return 	INT or FALSE
	 */
    public function add_template( $data )
    {
        return $this->db->insert( $this->template_table, $data );
    }

    /**
	 * 修改模板信息
	 *
	 * @param ARRAY $data
	 * @param INT $templateid
	 * @return BOOL
	 */
    public function edit_template( $data, $templateid )
    {
		return $this->db->simple_update( $this->template_table, $data, array( 'templateid' => $templateid ) );
    }

    /**
	 * 删除模板信息
	 *
	 * @param INT $templateid
	 * @return BOOL
	 */
    public function del_template( $templateid )
    {
        $templateid = intval( $templateid );
        return $this->edit_template( array( 'status' => STATUS_DEL ), $templateid );
    }

    /**
	 * 获取模板信息
	 *
	 * @param INT $templateid
	 * @return ARRAY $template
	 */
    public function get_template( $templateid )
    {
        $this->db->prepare( "SELECT * FROM $this->template_table where status = ? and templateid = ?" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_int( $templateid );
		$template = $this->db->execute( true );
		return $template;
    }

     /**
	 * 查询所有模板信息
	 *
	 * @param ARRAY() $params, STRING $order, INT $page, INT $pagesize
	 * @return 	ARRAY() $list
	 */
    public function get_template_list( $where, $page=null, $pagesize=null )
    {
        $sql = "SELECT * FROM $this->template_table WHERE status = ?";
        if( isset( $where['siteid'] ) && !empty( $where['siteid'] ) )
        {
            $sql .= " AND siteid = ? ";
        }
        if( isset( $page ) && isset( $pagesize ) )
        {
            $sql_count = "SELECT count(*) as nums FROM $this->template_table WHERE status = ?";
            if( isset( $where['siteid'] ) && !empty( $where['siteid'] ) )
            {
                $sql_count .= " AND siteid = ? ";
            }
            $this->db->prepare( $sql_count );
            $this->db->set_int( STATUS_NORMAL );
            if( isset( $where['siteid'] ) && !empty( $where['siteid'] ) )
            {
                $this->db->set_string( $where['siteid'] );
            }
            $res = $this->db->execute( true );
            $nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
            $this->pages = pages( $nums, $page, $pagesize );
            $sql .= " LIMIT ?,?";
        }
		$this->db->prepare( $sql );
        $this->db->set_int( STATUS_NORMAL );
        if( isset( $where['siteid'] ) && !empty( $where['siteid'] ) )
        {
            $this->db->set_string( $where['siteid'] );
        }
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }

    /**
	 * 添加模块信息
	 *
	 * @param ARRAY() $data
	 * @return 	INT or FALSE
	 */
    public function add( $data )
    {
        return $this->db->insert( $this->setting_table, $data );
    }

    /**
	 * 修改模块信息
	 *
	 * @param ARRAY $data
	 * @param INT $id
	 * @return BOOL
	 */
    public function edit( $data, $id )
    {
		return $this->db->simple_update( $this->setting_table, $data, array( 'id' => $id ) );
    }

    /**
	 * 删除模块信息
	 *
	 * @param INT $id
	 * @return BOOL
	 */
    public function del( $id )
    {
        $id = intval( $id );
        return $this->edit( array( 'status' => STATUS_DEL ), $id );
    }

    /**
	 * 获取模块信息
	 *
	 * @param INT $id
	 * @return ARRAY $data
	 */
    public function get( $id )
    {
        $this->db->prepare( "SELECT * FROM $this->setting_table where status = ? and id = ?" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_int( $id );
		$data = $this->db->execute( true );
		return $data;
    }
}
?>